package cn.dapeng.thread;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @BelongProject study
 * @BelongPackage cn.dapeng.thread
 * @Description TODO
 * @ClassName ThreadCommunicationT04
 * @Author xupenghong
 * @Date 2024/8/28 14:00
 * @Version 1.0.0
 */
public class ThreadCommunicationT06 {

    static BlockingQueue<String> q1 = new LinkedBlockingQueue<String>();

    static BlockingQueue<String> q2 = new LinkedBlockingQueue<String>();

    public static void main(String[] args) throws InterruptedException {
        char[] aI = "1234567".toCharArray();
        char[] bI = "ABCDEFG".toCharArray();

        new Thread(() -> {
            for (char c : aI) {
                try {
                    System.out.print(c);
                    q1.put("ok");
                    q2.take();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();

        new Thread(() -> {
            for (char c : bI) {
                try {
                    q1.take();
                    System.out.print(c);
                    q2.put("a");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();


    }

}
